package com.cdel.akka.chapter6;

import java.util.concurrent.TimeUnit;

import com.cdel.akka.chapter4.AkkademyDb;

import scala.concurrent.Await;
import scala.concurrent.Future;
import akka.actor.ActorIdentity;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Identify;
import akka.actor.Props;
import akka.pattern.AskableActorSelection;
import akka.util.Timeout;

public class AskTest {

	public static void main(String[] args) throws Exception {
		ActorSystem sys = ActorSystem.create("akkademy");
		sys.actorOf(Props.create(AkkademyDb.class), "mytest");

		//ActorSelection sel = sys.actorSelection("/user/mytest");
		ActorSelection sel = sys.actorSelection("akka.tcp://akkademy@127.0.0.10:52671/user/mytest");

		Timeout t = new Timeout(5, TimeUnit.SECONDS);
		AskableActorSelection asker = new AskableActorSelection(sel);
		Future<Object> fut = asker.ask(new Identify(1), t);
		ActorIdentity ident = (ActorIdentity) Await.result(fut, t.duration());
		ActorRef ref = ident.getRef();
		System.out.println(ref == null);
	}
}